package com.chushouya.order.service.admin;

import com.general.framework.core.exception.BaseException;
import com.general.framework.core.exception.Ex;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;


@Service
@Slf4j
public class AlipayGatewayTypeService {

//    @Resource
//    private AntMerchantShopService antMerchantShopService;

//    @Resource
//    private AlipayGatewayService alipayGatewayService;
//
//
//    @Resource
//    private AlipayOpenMiniOrderChangedService alipayOpenMiniOrderService;
//
//    @Resource
//    private AlipayOpenMiniOrderAftersaleChangedService alipayOpenMiniOrderAftersaleService;

    private Map<String, Consumer<Map>> grantTypeMap = new HashMap<>();

    /**
     * 初始化订单逻辑,代替了if-else部分
     * key: 订单操作类型
     * value: lambda表达式,操作订单
     */
    @PostConstruct
    public void dispatcherInit() {

        //门店创建成功通知
//        grantTypeMap.put(AlipayGatewayMethodEnum.ANT_MERCHANT_EXPAND_SHOP_SAVE_PASSED.value(), data -> antMerchantShopService.antMerchantShopPassed(data));

        //门店创建失败通知
//        grantTypeMap.put(AlipayGatewayMethodEnum.ANT_MERCHANT_EXPAND_SHOP_SAVE_REJECTED.value(), data -> antMerchantShopService.antMerchantShopRejected(data));

//        //回收行业森林能量发放结果通知
//        grantTypeMap.put(AlipayGatewayMethodEnum.ALIPAY_COMMERCE_RECYCLE_ENERGY_NOTIFY.value(), data -> alipayGatewayService.alipayCommerceRecycleEnergyNotify(data));
//
//        //支付宝钱包开通通知
//        grantTypeMap.put(AlipayGatewayMethodEnum.ALIPAY_FUND_WALLET_OPEN_COMPLETED.value(), data -> alipayWalletNotifyService.alipayFundWalletOpenCompleted(data));
//
//        //钱包提现异步通知
//        grantTypeMap.put(AlipayGatewayMethodEnum.ALIPAY_FUND_WALLET_WITHDRAW_COMPLETED.value(), data -> alipayWalletNotifyService.alipayFundWalletWithdrawCompleted(data));
//
//        //钱包退款异步通知
//        grantTypeMap.put(AlipayGatewayMethodEnum.ALIPAY_FUND_WALLET_REFUND_COMPLETED.value(), data -> alipayWalletNotifyService.alipayFundWalletRefundCompleted(data));
//
//        //支付宝转账订单状态变更通知
//        grantTypeMap.put(AlipayGatewayMethodEnum.ALIPAY_FUND_TRANS_ORDER_CHANGED.value(), data -> alipayWalletNotifyService.alipayFundTransOrderChanged(data));
//
//        //支付宝钱包支付完成通知
//        grantTypeMap.put(AlipayGatewayMethodEnum.ALIPAY_FUND_WALLET_PAYMENT_COMPLETED.value(), data -> alipayWalletNotifyService.alipayFundWalletPaymentCompleted(data));
//
//        //订单售后消息通知
//        grantTypeMap.put(AlipayGatewayMethodEnum.ALIPAY_OPEN_MINI_ORDER_AFTERSALE_CHANGED.value(),data->alipayOpenMiniOrderAftersaleService.alipayOpenMiniOrderAftersale(data));
//
//        //订单变更通知
//        grantTypeMap.put(AlipayGatewayMethodEnum.ALIPAY_OPEN_MINI_ORDER_CHANGED.value(),data->alipayOpenMiniOrderService.alipayOpenMiniOrderChanged(data));


    }

    public void getResult(Map<String, String> params) {
        try {
            Consumer<Map> result = grantTypeMap.get(params.get("msg_method"));
            if (Objects.isNull(result)) {
                log.error("网关操作不存在 {}", params.get("msg_method"));
                throw Ex.business("网关操作不存在 {}", params.get("msg_method"));
            }
            result.accept(params);
        } catch (BaseException e) {
            log.info("网关操作失败：{}", e.getMessage());
        } catch (Exception e) {
            log.error("网关操作失败：{}", e.getMessage());
        }

    }
}
